#include "progressdialog.h"
#include "ui_progressdialog.h"

ProgressDialog::ProgressDialog(const QString title, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProgressDialog),
    model(new QStandardItemModel(0,1))
{
    ui->setupUi(this);
    setWindowTitle(title);
    ui->progressBar->setValue(0);
    ui->labelCurrentAction->setText("");
    ui->listView->setModel(model);
    ui->pushButtonClose->setIcon(QIcon::fromTheme("dialog-close"));
}

ProgressDialog::~ProgressDialog()
{
    delete ui;
}

void ProgressDialog::on_progressBar_valueChanged(int value)
{
    ui->pushButtonClose->setEnabled( value == 100 );
}

void ProgressDialog::setValue(int value, QString actionText, QString logText)
{
    if (value < ui->progressBar->value()) emit backstep(ui->progressBar->value() - value);
    ui->progressBar->setValue(value);
    ui->labelCurrentAction->setText(actionText);
    model->insertRow(model->rowCount());
    model->setData(model->index(model->rowCount()-1,0),logText);
    if ((logText.split(' ').at(logText.split(' ').count()-1) == tr("COMPLITED")) or (logText == tr("Operation finished") )) {
        model->itemFromIndex(model->index(model->rowCount()-1,0))->setIcon(QIcon::fromTheme("dialog-ok"));
    } else {
        model->itemFromIndex(model->index(model->rowCount()-1,0))->setIcon(QIcon::fromTheme("dialog-warning"));
    }
}

int ProgressDialog::value()
{
    return ui->progressBar->value();
}
